package com.leyou.item.service;

import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exception.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.item.pojo.dto.CategoryDTO;
import com.leyou.item.entity.Category;
import com.leyou.item.mapper.CategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    public List<CategoryDTO> queryListByParent(Long pid) {
        /*设置查询条件*/
        Category t = new Category();
        t.setParentId(pid);
        /*查询*/
        List<Category> list = categoryMapper.select(t);
        /*如果结果为空，抛出自定义异常*/
        if (CollectionUtils.isEmpty(list)) {
            throw new LyException(ExceptionEnum.CATEGORY_NOT_FOUND);
        }

        /*将 List<Category> 转换为 List<CategoryDTO>*/
        return BeanHelper.copyWithCollection(list, CategoryDTO.class);
    }

    public void addChildNode(CategoryDTO categoryDTO) {
         /* node信息
          id: 0   //父节点信息
          isParent: false   //是否是父节点
          name: "新的节点"   //名字
          parentId: 1      //父节点id
          sort: 13*/      //顺序
        Long parentId = categoryDTO.getParentId();
        /*查询父节点信息*/
        Category categoryParent = categoryMapper.selectByPrimaryKey(parentId);
        /*如果当前节点的父节点是根节点，就将当前节点设置为父节点*/
        if (categoryParent.getParentId() == 0) {
            categoryDTO.setIsParent(true);
        }

        Category category = BeanHelper.copyProperties(categoryDTO, Category.class);

        int number = categoryMapper.insert(category);
        if (number != 1) {
            throw new LyException(ExceptionEnum.CATEGORY_INSERT_FAIL);
        }

    }

    /*删除节点*/
    public void deleteById(Long id) {
        int number = categoryMapper.deleteByPrimaryKey(id);
        if (number != 1) {
            throw new LyException(ExceptionEnum.CATEGORY_DELETE_FAIL);
        }
    }

    /*根据品牌id查询品牌所属分类信息*/
    public List<CategoryDTO> queryListByBrandId(Long brandId) {
        List<Category> list = categoryMapper.queryByBrandId(brandId);
        if (CollectionUtils.isEmpty(list)) {
            throw new LyException(ExceptionEnum.CATEGORYS_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(list, CategoryDTO.class);
    }

    /*查询商品分类，传进来的参数是商品分类的id集合，返回的是商品分类集合*/
    public List<CategoryDTO> queryCategoryByIds(List<Long> cids){
        List<Category> list = categoryMapper.selectByIdList(cids);
        if (CollectionUtils.isEmpty(list)){
            throw new LyException(ExceptionEnum.RESOURCE_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(list,CategoryDTO.class);
    }

    /**
     * 根据三级分类id查询所有分类信息
     * @return
     */
    public List<CategoryDTO> queryAllByCid3(Long cid) {
        List<Category> categoryList = new ArrayList<>();
        //查询三级分类信息
        Category c3 = categoryMapper.selectByPrimaryKey(cid);
        if (c3 == null){
            throw new LyException(ExceptionEnum.RESOURCE_NOT_FOUND);
        }
        Category c2 = categoryMapper.selectByPrimaryKey(c3.getParentId());
        if (c2 == null){
            throw new LyException(ExceptionEnum.RESOURCE_NOT_FOUND);
        }
        Category c1 = categoryMapper.selectByPrimaryKey(c2.getParentId());
        if (c1 == null){
            throw new LyException(ExceptionEnum.RESOURCE_NOT_FOUND);
        }

        categoryList.add(c3);
        categoryList.add(c2);
        categoryList.add(c1);

        return BeanHelper.copyWithCollection(categoryList,CategoryDTO.class);
    }
}
